﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Nevron.Chart;
using Nevron.GraphicsCore;
using Nevron.Chart.WinForm;
using Nevron;
using System.IO;
using System.Configuration;
using Nevron.UI;
using Nevron.Editors;
using System.Drawing;

namespace IGuess
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.Text = RM.GetString("MainFormTitle");
            this.fileToolStripMenuItem.Text = RM.GetString("MainFormFileMenuItem");
            this.aboutToolStripMenuItem.Text = RM.GetString("MainFormAboutToolStrip");
            this.helpToolStripMenuItem.Text = RM.GetString("MainFormHelpToolStrip");
            this.exitToolStripMenuItem.Text = RM.GetString("MainFormExitToolStrip");
            this.mainFormIntroductionTextArea.Text = RM.GetString("MainFormIntroductionTextArea");
            this.mainFormIntroductionTextArea.TextAlign = HorizontalAlignment.Center;
            this.mainFormInitiateClassifierButton.Text = RM.GetString("mainFormInitiateClassifierButton");
            this.mainFormClassificationStatsGroupBox.Text = RM.GetString("mainFormClassificationStatsGroupBox");
            this.mainFormLanguagePercentageGroupBox.Text = RM.GetString("mainFormLanguagePercentageGroupBox");
            
            this.generateStatsPieChart();
            this.generateStatsBarChart();

        }

        public void generateStatsPieChart()
        {

            nChartControl1.Controller.Tools.Add(new NSelectorTool());
            nChartControl1.Controller.Tools.Add(new NTrackballTool());

            NPieChart chart = new NPieChart();

            nChartControl1.Charts.Clear();
            nChartControl1.Charts.Add(chart);
            chart.Enable3D = true;
            chart.Width = 70;
            chart.Depth = 10;
            chart.Projection.SetPredefinedProjection(PredefinedProjection.PerspectiveElevated);
            chart.LightModel.SetPredefinedLightModel(PredefinedLightModel.ShinyCameraLight);

            this.pie = (NPieSeries)chart.Series.Add(SeriesType.Pie);
            this.pie.PieStyle = PieStyle.SmoothEdgePie;
            this.pie.LabelMode = PieLabelMode.SpiderNoOverlap;

            GenerateDataPieChart(this.pie);

            NStyleSheet styleSheet = NStyleSheet.CreatePredefinedStyleSheet(PredefinedStyleSheet.NevronMultiColor);
            styleSheet.Apply(nChartControl1.Document);

            nChartControl1.Refresh();

        }

        private void GenerateDataPieChart(NPieSeries pie)
        {
            if (File.Exists(ConfigurationManager.AppSettings["StatisticsFile"]))
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(ConfigurationManager.AppSettings["StatisticsFile"]))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] text = line.Split();
                        pie.AddDataPoint(new NDataPoint(Convert.ToDouble(text[1]), text[0]));
                    }

                    this.nChartControl1.Visible = true;
                    this.textBox1.Visible = false;
                    this.mainFormAlanizedTextsPictureBox.Visible = false;
                }
            }
            else
            {
                this.nChartControl1.Visible = false;
                this.mainFormAlanizedTextsPictureBox.Visible = true;
                this.textBox1.Text = "File not Found";
                this.textBox1.Visible = true;
                this.textBox1.TextAlign = HorizontalAlignment.Center;
                this.textBox1.BackColor = Color.Yellow;
            }
        }

        public void generateStatsBarChart()
        {

            nChartControl2.Settings.JitterMode = JitterMode.Enabled;
            nChartControl2.BackgroundStyle.FrameStyle.Visible = false;
            nChartControl2.Legends[0].Visible = false;
            // configure the chart
            NChart chart = nChartControl2.Charts[0];
            chart.Enable3D = true;
            // configure X axis
            NOrdinalScaleConfigurator scaleX = new NOrdinalScaleConfigurator();
            scaleX.AutoLabels = false;
            scaleX.Labels.Add("EN");
            scaleX.Labels.Add("DE");
            scaleX.Labels.Add("FR");
            //scaleX.
            scaleX.DisplayDataPointsBetweenTicks = false;
            chart.Axis(StandardAxis.PrimaryX).ScaleConfigurator = scaleX;

            // configure Y axis
            NLinearScaleConfigurator scaleY = new NLinearScaleConfigurator();
            scaleY.MajorGridStyle.LineStyle.Pattern = LinePattern.Dot;
            scaleY.MajorGridStyle.ShowAtWalls = new ChartWallType[] { ChartWallType.Back };
            chart.Axis(StandardAxis.PrimaryY).ScaleConfigurator = scaleY;

            // add interlaced stripe for Y axis
            NScaleStripStyle stripStyle = new NScaleStripStyle(new NColorFillStyle(Color.Beige), null, true, 0, 0, 1, 1);
            stripStyle.Interlaced = true;
            stripStyle.ShowAtWalls = new ChartWallType[] { ChartWallType.Back };
            scaleY.StripStyles.Add(stripStyle);

            // bar series
            NBarSeries series1 = (NBarSeries)chart.Series.Add(SeriesType.Bar);

            series1.DataLabelStyle.Visible = true;
            series1.DataLabelStyle.VertAlign = VertAlign.Top;
            series1.DataLabelStyle.ArrowLength = new NLength(10);
            series1.DataLabelStyle.TextStyle.FontStyle.EmSize = new NLength(8);

            // enable / disable initial labels positioning
            chart.LabelLayout.EnableInitialPositioning = true;

            // enable / disable removal of overlapped labels after initial positioning
            chart.LabelLayout.RemoveOverlappedLabels = true;

            // enable / disable label adjustment
            chart.LabelLayout.EnableLabelAdjustment = true;

            // use only `$:5`  location for the labels
            series1.LabelLayout.UseLabelLocations = true;

            //series1.LabelLayout.LabelLocations = new LabelLocation[] { LabelLocation.Top };

            series1.LabelLayout.OutOfBoundsLocationMode = OutOfBoundsLocationMode.PushWithinBounds;
            series1.LabelLayout.InvertLocationsIfIgnored = true;

            // protect data points from being overlapped
            series1.LabelLayout.EnableDataPointSafeguard = true;
            series1.LabelLayout.DataPointSafeguardSize = new NSizeL(
                new NLength(1.3f, NRelativeUnit.ParentPercentage),
                new NLength(1.3f, NRelativeUnit.ParentPercentage));

            series1.Values.Clear();

            if (File.Exists(ConfigurationManager.AppSettings["StatisticsFile"]))
            {
                using (StreamReader sr = new StreamReader(ConfigurationManager.AppSettings["StatisticsFile"]))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] text = line.Split();
                        Double languagePercentage = (Convert.ToDouble(text[2]) / Convert.ToDouble(text[1])) * 100;
                        series1.Values.Add(languagePercentage);
                    }
                    this.nChartControl2.Visible = true;
                    this.mainFormStatsPictureBox.Visible = false;
                    this.textBox2.Visible = false;

                }
            }
            else
            {
                this.nChartControl2.Visible = false;
                this.mainFormStatsPictureBox.Visible = true;
                this.textBox2.Text = "File not found.";
                this.textBox2.Visible = true;
                this.textBox2.TextAlign = HorizontalAlignment.Center;
                this.textBox2.BackColor = Color.Yellow;
            }

            nChartControl2.Update();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(RM.GetString("AboutMessage"));
        }

        private void mainFormInitiateClassifierButton_Click(object sender, EventArgs e)
        {

            this.classifierForm = new ClassifierForm(this);
            this.classifierForm.Visible = true;
            this.Visible = false;
            this.Enabled = false;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void helpToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void MainForm_EnabledChanged(object sender, EventArgs e)
        {
            this.generateStatsPieChart();
            this.generateStatsBarChart();
        }

    }
}
